#include "CTimeAxis.h"
#include <QDateTime>

CTimeAxis::CTimeAxis()
{
    setMaximumHeight(60);

    m_bIsDragging = false;
    m_nMultiple = 0;
    m_nStepPx = 1;//1秒



    QDateTime current = QDateTime::currentDateTime();
    m_strCurTime = current.toString("yyyy-MM-dd hh:mm:ss");
}



void CTimeAxis::paintEvent(QPaintEvent*)
{


    QPainter p(this);
    // drawScale(&p);  // 绘制刻度
    // drawNodes(&p);  // 绘制时间节点

    //p.fillRect(rect(), Qt::black);

    p.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    //绘制圆角背景
    QPainterPath backgroundPath;
    backgroundPath.addRoundedRect(rect(), 5, 5);  // 圆角半径5px
    p.fillPath(backgroundPath, QBrush(Qt::black));



    p.setPen(QPen(Qt::white, 1));


    //1.中心点时间
    //2.起始结束时间 从左到右绘制
    //3.缩放模式，1分钟、10分钟、1小时 //12个小时，6个小时，2个小时， 1个小时， 30分钟， 10分钟， 5分钟， 1分钟


    qDebug() << "当前时间：" << m_strCurTime;

    // QString timeStr = "2025-07-17 14:30";  // 示例时间字符串
    QDateTime datetime = QDateTime::fromString(m_strCurTime, "yyyy-MM-dd HH:mm:ss");
    qint64 int64curtimestamp = datetime.toSecsSinceEpoch();  // 获取秒级时间戳

    int ncenterx = width()/2;
    qint64 int64startimestamp = int64curtimestamp - (ncenterx * m_nStepPx); // width()/2 * steppx
    qint64 int64endtimestamp = int64curtimestamp + (ncenterx * m_nStepPx);

    QDateTime datestarttime = QDateTime::fromSecsSinceEpoch(int64startimestamp);
    m_strStartTime = datestarttime.toString("yyyy-MM-dd hh:mm:ss");
    QDateTime dateendtime = QDateTime::fromSecsSinceEpoch(int64endtimestamp);
    m_strEndTime = dateendtime.toString("yyyy-MM-dd hh:mm:ss");


    //得到第一个长条点的位置 15:55:33这个开始时间，如果是1s的像素步长，60像素1个长条点
    //ss不为00，替换为00，mm+1得到 hh:mm:00的点
    QString strfirsttime;
    int nfirstposx;//距离左侧距离
    qint64 int64firsttimestamp;
    if(m_nStepPx > 0 && m_nStepPx < 60)
    {
        QDateTime datefirsttime = datestarttime;

        // 获取时间部分
        QTime time = datefirsttime.time();

        // 如果秒数不为0，则设置为0
        if (time.second() != 0) {
            time.setHMS(time.hour(), time.minute()+1, 0);
            datefirsttime.setTime(time);
        }
        strfirsttime = datefirsttime.toString("yyyy-MM-dd hh:mm:ss");

        int64firsttimestamp = datefirsttime.toSecsSinceEpoch();  // 获取秒级时间戳

        nfirstposx = (int64firsttimestamp - int64startimestamp) / m_nStepPx; //时间差/步长 = 像素距离
    }
    else
    {
        //1px=60s  1格为一小时， 显示hh:00
        QDateTime datefirsttime = datestarttime;

        // 获取时间部分
        QTime time = datefirsttime.time();

        // 如果分钟数不为0，则设置为0
        if (time.minute() != 0) {
            time.setHMS(time.hour()+1, 0, 0);
            datefirsttime.setTime(time);
        }

        strfirsttime = datefirsttime.toString("yyyy-MM-dd hh:mm:ss");
        int64firsttimestamp = datefirsttime.toSecsSinceEpoch();  // 获取秒级时间戳
        nfirstposx = (int64firsttimestamp - int64startimestamp) / m_nStepPx;
    }

    int nscalecount = width() / 10; //每个10px一个刻度

    int ncount6 = 0;
    for(int i=0; i<nscalecount ;i++)
    {
        if(ncount6 == 0)
        {
            //x1y1x2y2
            p.drawLine(nfirstposx + (i*10), 45, nfirstposx + (i*10), height());
            //写上时间点，计算时间点
            //距离中心点的像素，得到时间差

            qint64 int64timestamp = int64firsttimestamp + ((i*10) * m_nStepPx);
            QDateTime datetime1 = QDateTime::fromSecsSinceEpoch(int64timestamp);



            QTime time = datetime1.time();
            if (time.minute() == 0 && time.hour() == 0)
            {

                //00:00:00
                QString timePart1MMdd = datetime1.date().toString("MM-dd");
                p.drawText( (nfirstposx + (i*10))-15, 40, timePart1MMdd);
            }
            else
            {
                QString timePart1hhmm = datetime1.time().toString("hh:mm");
                p.drawText( (nfirstposx + (i*10))-15, 40, timePart1hhmm);
            }
        }
        else
        {
            p.drawLine(nfirstposx + (i*10) , 55, nfirstposx + (i*10) , height());

        }

        ncount6++;
        if(ncount6 == 6)
        {
            ncount6 = 0;
        }

    }

    //显示当前时间点
    p.drawText( (width()/2)-60, 15, m_strCurTime);

    //中心点三角形箭头
    //p.setRenderHint(QPainter::Antialiasing); // 启用抗锯齿

    // 定义三角形三个顶点坐标
    QPointF points[3] = {
        QPointF((width()/2)-10, 20),  // 顶点1
        QPointF((width()/2)+10, 20),  // 顶点2
        QPointF((width()/2), 30)   // 顶点3
    };

    // 设置画笔和画刷
    p.setPen(QPen(Qt::yellow, 1));      // 蓝色边框，线宽2像素
    p.setBrush(QBrush(Qt::yellow));    // 黄色填充

    // 绘制三角形
    p.drawConvexPolygon(points, 3);   // 使用3个顶点绘制

    //绘制竖线，指示线
    p.setPen(QPen(Qt::yellow, 1));
    p.setBrush(Qt::NoBrush);
    p.drawLine((width()/2), 30, (width()/2), height());
}

void CTimeAxis::wheelEvent(QWheelEvent *event)
{
    //12个小时，6个小时，2个小时， 1个小时， 30分钟， 10分钟， 5分钟， 1分钟
    //7 6 5 4 3 2 1 0
    int delta = event->angleDelta().y();
    // 获取垂直滚轮增量
    if(delta > 0)
    {
        if(m_nMultiple == 0)
        {
            return;
        }
        m_nMultiple--;
        qDebug() << "向上滚动"; // 放大操作
    }
    else
    {
        if(m_nMultiple == 4)
        {
            return;
        }
        m_nMultiple++;

        qDebug() << "向下滚动"; // 缩小操作
    }


    //60像素为1格，
    if(m_nMultiple == 0)
    {
        m_nStepPx = 1; //1s ，1px=1s 1格1分钟
    }
    else if(m_nMultiple == 1)
    {
        m_nStepPx = 5;//5s  1px=5s  1格5分钟
    }
    else if(m_nMultiple == 2)
    {
        m_nStepPx = 10;//10s 1px=10s  1格10分钟
    }
    else if(m_nMultiple == 3)
    {
        m_nStepPx = 30;//30s  1px=30s  1格30分钟
    }
    else if(m_nMultiple == 4)
    {
        m_nStepPx = 60;//60s  1px=60s  1格60分钟
    }
    // else if(m_nMultiple == 5)
    // {
    //     m_nStepPx = 60 * 2;//120s  1px=120s  1格120分钟
    // }
    // else if(m_nMultiple == 6)
    // {
    //     m_nStepPx = 60 * 6;//360s 1px=360s  1格6小时
    // }
    // else if(m_nMultiple == 7)
    // {
    //     m_nStepPx = 60 * 12;//720s 1px=720s  1格12小时
    // }


    update(); // 请求重绘

}

void CTimeAxis::mouseMoveEvent(QMouseEvent *event)
{
    if (m_bIsDragging)
    {
        int dx = event->pos().x() - m_lastMousePos.x();
        //m_offset += dx;
        QDateTime datetime = QDateTime::fromString(m_strCurTime, "yyyy-MM-dd HH:mm:ss");
        qint64 int64curtimestamp = datetime.toSecsSinceEpoch();  // 获取秒级时间戳
        qint64 int64curtimestamp2;
        int64curtimestamp2 = int64curtimestamp - (dx * m_nStepPx);

        QDateTime dateendtime1 = QDateTime::fromSecsSinceEpoch(int64curtimestamp2);
        m_strCurTime = dateendtime1.toString("yyyy-MM-dd hh:mm:ss");
        update(); // 触发重绘
        //emit timeChanged(calculateCurrentTime());
    }
    m_lastMousePos = event->pos();
}

void CTimeAxis::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_bIsDragging = true;
        m_lastMousePos = event->pos();
        setCursor(Qt::ClosedHandCursor);
    }
}

void CTimeAxis::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_bIsDragging = false;
        setCursor(Qt::ArrowCursor);


        emit TimeChange(m_strCurTime);
    }
}
